<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html" charset="utf-8"/>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"/>
    <meta http-equiv="X-UA-Compatible" content="ie=edge"/>
    <title>Title</title>
</head>
<body>

<script>
    var menus = [
        {code: '1', hidden: false},
        {
            code: '2', hidden: false, children: [
                {code: '2.1', hidden: false},
                {code: '2.2', hidden: false},
                {code: '2.3', hidden: false}
            ]
        },
        {code: '3', hidden: false, children: [
                {code: '3.1', hidden: false},
                {code: '3.2', hidden: false}
            ]},
    ];
    var rightMenus = [
        {menuCode: '1', hidden: false},
        {menuCode: '2.1', hidden: false},
        {menuCode: '2.2', hidden: false},
        {menuCode: '3', hidden: false},
    ];


    function getRightMenus() {
        checkRightMenus(menus, rightMenus);
    }

    getRightMenus();

    /**
     *
     * @param menus 所有菜单
     * @param rightMenus 后端返回具有权限的菜单
     * @returns {boolean}
     */
    function checkRightMenus(menus, rightMenus) {
        if(!menus || menus.length === 0)
            return true;

        let hasShow = false;
        for (let i = 0; i < menus.length; i++) {
            let menu = menus[i];

            if (menu.children && menu.children !== 0)
                //有子节点, 至少有一个孩子节点显示, 才显示
                menu.hidden = !checkRightMenus(menu.children, rightMenus);
            else{
                //无子节点
                menu.hidden = !checkRight(menu, rightMenus)
            }

            if (!menu.hidden) hasShow = true;
        }
        return hasShow;
    }

    /**
     * 检查Menu是否具有权限
     * @param menu
     * @param rightMenus
     * @returns {boolean}
     */
    function checkRight(menu, rightMenus) {
        for (let i = 0; i < rightMenus.length; i++) {
            let rightMenu = rightMenus[i];
            if (menu.code === rightMenu.menuCode){
                return true;
            }
        }
        return false;
    }
</script>
<script src="lib/jquery-3.5.1.js"></script>
</body>
</html>